package team.mutong.taxi.utils;

import java.util.Collections;
import java.util.List;
import java.util.Map;

import team.mutong.taxi.domain.DtwFeature;
import team.mutong.taxi.domain.GPSPoint;

/**
 * @author TomWang
 * @email wanghuochen@outlook.com
 * @date 2016年5月15日 下午6:26:04
 * @version 1.0
 */
public class GetFeature {

	/**
	 * 特征CALL_TYPE
	 * @param dewFeatemp
	 * @return
	 * @author TomWang
	 */
	public static Double getCallTpye(DtwFeature dewFeatemp) {
		Map<String, Object> data = dewFeatemp.getData();
		String resule = (String) data.get("CALL_TYPE");
		switch (resule) {
		case "A":
			resule = "1";
			break;
		case "B":
			resule = "2";
			break;
		case "C":
			resule = "3";
			break;
		}
		return Double.parseDouble(resule);
	}
	/**
	 * 特征TAXT_ID
	 * @param dewFeatemp
	 * @return
	 * @author TomWang
	 */
	public static double getTaxiId(DtwFeature dewFeatemp) {
		Map<String, Object> data = dewFeatemp.getData();
		String resule = (String) data.get("TAXI_ID");
		return Double.parseDouble(resule);
	}
	/**
	 * 特征TIMESTAMP
	 * @param dewFeatemp
	 * @return
	 * @author TomWang
	 */
	public static double getTimeStamp(DtwFeature dewFeatemp) {
		Map<String, Object> data = dewFeatemp.getData();
		String resule = (String) data.get("TIMESTAMP");
		return Double.parseDouble(resule);
	}
	/**
	 * 特征DAY_TYPE
	 * @param dewFeatemp
	 * @return
	 * @author TomWang
	 */
	public static double getDayTpye(DtwFeature dewFeatemp) {
		Map<String, Object> data = dewFeatemp.getData();
		String resule = (String) data.get("DAY_TYPE");
		switch (resule) {
		case "A":
			resule = "1";
			break;
		case "B":
			resule = "2";
			break;
		case "C":
			resule = "3";
			break;
		}
		return Double.parseDouble(resule);
	}
	/**
	 * 特征ORIGIN_CALL
	 * @param dewFeatemp
	 * @return
	 * @author TomWang
	 */
	public static double getOriginCall(DtwFeature dewFeatemp) {
		Map<String, Object> data = dewFeatemp.getData();
		String resule = (String) data.get("ORIGIN_CALL");
		if ("".equals(resule)||"NA".equals(resule)) {
			resule = "0";
		}
		return Double.parseDouble(resule);
	}
	/**
	 * 特征ORIGIN_STAND
	 * @param dewFeatemp
	 * @return
	 * @author TomWang
	 */
	public static double getOriginStand(DtwFeature dewFeatemp) {
		Map<String, Object> data = dewFeatemp.getData();
		String resule = (String) data.get("ORIGIN_STAND");
		if ("".equals(resule)) {
			resule = "0";
		}
		if("NA".equals(resule)){
			resule = "0";
		}
		return Double.parseDouble(resule);
	}
	/**
	 * 特征时间A,表示b到c的时间 ,b为c为截取段
	 * a------b-------c
	 * @param dewFeatemp
	 * @return
	 * @author TomWang
	 */
	public static double getTimeA(DtwFeature dewFeatemp) {
		double reuslt = 0;
		List<GPSPoint> pointList = dewFeatemp.getCutRoad();
		reuslt = (pointList.size() - 1) * 15;// 每15秒钟打一个点计算点的个数，可以得到时间
		return reuslt;
	}
	/**
	 * 特征时间B,表示a到b的时间 ,b为c为截取段
	 * a------b-------c
	 * @param dewFeatemp
	 * @return
	 * @author TomWang
	 */
	public static double getTimeB(DtwFeature dewFeatemp) {
		double reuslt = 0;
		int i;
		int dir = dewFeatemp.getDir();
		List<GPSPoint> pointList = dewFeatemp.getCutRoad();
		Map<String, Object> data = dewFeatemp.getData();
		String testSetRoad = ChangeType.blobChangetoString((byte[]) data.get("POLYLINE"));//取出测试用的路径
		List<GPSPoint> testSetGpsPoint = ChangeType.changeToObject(testSetRoad);//将坐标转换成二维数组
		if(dir==0){
			Collections.reverse(testSetGpsPoint);
			for(i=0; i<testSetGpsPoint.size(); i++){
				if(pointList.get(0).equals(testSetGpsPoint.get(i))){
					break;
				}
			}
		}else{
			for(i=0; i<testSetGpsPoint.size(); i++){
				if(pointList.get(0).equals(testSetGpsPoint.get(i))){
					break;
				}
			}
		}
		reuslt = i * 15;// 每15秒钟打一个点计算点的个数，可以得到时间
		return reuslt;
	}
	/**
	 * 特征时间C,表示a到c的时间 ,b为c为截取段
	 * a------b-------c
	 * @param dewFeatemp
	 * @return
	 * @author TomWang
	 */
	public static double getTimeC(DtwFeature dewFeatemp) {
		return getTimeA(dewFeatemp)+getTimeB(dewFeatemp);
	}
	/**
	 * 特征平均速度A, 表示b到c的平均速度 ,b为c为截取段
	 * a------b-------c
	 * @param dewFeatemp
	 * @return
	 * @author TomWang
	 */
	public static double getSpeedA(DtwFeature dewFeatemp) {
		double result = getDistenceA(dewFeatemp) / getTimeA(dewFeatemp);
		if(getTimeA(dewFeatemp)==0){
			result=0;
		}
		return result;
	}
	/**
	 * 特征平均速度B, 表示a到b的平均速度 ,b为c为截取段
	 * a------b-------c
	 * @param dewFeatemp
	 * @return
	 * @author TomWang
	 */
	public static double getSpeedB(DtwFeature dewFeatemp) {
		double result = getDistenceB(dewFeatemp) / getTimeB(dewFeatemp);
		if(getTimeB(dewFeatemp)==0){
			result=0;
		}
		return result;
	}
	/**
	 * 特征平均速度C, 表示a到c的平均速度 ,b为c为截取段
	 * a------b-------c
	 * @param dewFeatemp
	 * @return
	 * @author TomWang
	 */
	public static double getSpeedC(DtwFeature dewFeatemp) {
		double result = getDistenceC(dewFeatemp) / getTimeC(dewFeatemp);
		if(getTimeC(dewFeatemp)==0){
			result=0;
		}
		return result;
	}
	/**
	 * 特征距离A,表示b到c的距离 ,b为c为截取段
	 * a------b-------c
	 * @param dewFeatemp
	 * @return
	 * @author TomWang
	 */
	public static double getDistenceA(DtwFeature dewFeatemp) {
		double reuslt = dewFeatemp.getDistence();
		return reuslt;
	}
	/**
	 * 特征距离B,表示a到b的距离 ,b为c为截取段
	 * a------b-------c
	 * @param dewFeatemp
	 * @return
	 * @author TomWang   
	 */
	public static double getDistenceB(DtwFeature dewFeatemp) {
		double reuslt = 0;
		int i;
		int dir = dewFeatemp.getDir();
		List<GPSPoint> pointList = dewFeatemp.getCutRoad();
		Map<String, Object> data = dewFeatemp.getData();
		String testSetRoad = ChangeType.blobChangetoString((byte[]) data.get("POLYLINE"));		//取出测试用的路径
		List<GPSPoint> testSetGpsPoint = ChangeType.changeToObject(testSetRoad);//将坐标转换成二维数组
		if(dir==0){
			Collections.reverse(testSetGpsPoint);
			for(i=0; i<testSetGpsPoint.size(); i++){
				if(pointList.get(0).equals(testSetGpsPoint.get(i))){
					break;
				}
			}
		}else{
			for(i=0; i<testSetGpsPoint.size(); i++){
				if(pointList.get(0).equals(testSetGpsPoint.get(i))){
					break;
				}
			}
		}
		for(int j=1; j<i; j++){
			reuslt += EuclideanMetric.getEDistence(testSetGpsPoint.get(i-1), testSetGpsPoint.get(i));
		}
		return reuslt;
	}
	/**
	 * 特征距离C,表示a到c的距离 ,b为c为截取段
	 * a------b-------c
	 * @param dewFeatemp
	 * @return
	 * @author TomWang
	 */
	public static double getDistenceC(DtwFeature dewFeatemp) {
		return getDistenceA(dewFeatemp)+getDistenceB(dewFeatemp);
	}
	/**
	 * 得到截取段的终点，即c点的gps坐标，b为c为截取段
	 * a------b-------c-------e
	 * @param dewFeatemp
	 * @return
	 * @author TomWang
	 */
	public static GPSPoint getCutEndPoint(DtwFeature dewFeatemp) {
		List<GPSPoint> pointList = dewFeatemp.getCutRoad();
		 double dis= dewFeatemp.getDistence();
		if(dis == 0) {
			GPSPoint temp = new GPSPoint(0,0);
			return temp;
		}else{			
			return pointList.get(pointList.size() - 1);
		}		
	}

	/**
	 * 得到截取段的起点，即b点的gps坐标，b为c为截取段
	 * a------b-------c-------e
	 * @param dewFeatemp
	 * @return
	 * @author TomWang
	 */
	public static GPSPoint getCutStartPoint(DtwFeature dewFeatemp) {
		List<GPSPoint> pointList = dewFeatemp.getCutRoad();
		double dis= dewFeatemp.getDistence();
		if(dis == 0) {
			GPSPoint temp = new GPSPoint(0,0);
			return temp;
		}else{			
			return  pointList.get(0);
		}
	}
	/**
	 * 得到匹配段的起点，即a点的gps坐标，b为c为截取段
	 * a------b-------c-------e
	 * @param dewFeatemp
	 * @return
	 * @author TomWang
	 */
	public static GPSPoint getStartPoint(DtwFeature dewFeatemp) {
		int dir = dewFeatemp.getDir();
		Map<String, Object> data = dewFeatemp.getData();
		String testSetRoad = ChangeType.blobChangetoString((byte[]) data.get("POLYLINE"));		//取出测试用的路径
		List<GPSPoint> testSetGpsPoint = ChangeType.changeToObject(testSetRoad);//将坐标转换成二维数组
		if(dir==0){
			Collections.reverse(testSetGpsPoint);
		}
		return testSetGpsPoint.get(0);
	}
	/**
	 * 得到匹配段的终点，即e点的gps坐标，b为c为截取段
	 * a------b-------c-------e
	 * @param dewFeatemp
	 * @return
	 * @author TomWang
	 */
	public static GPSPoint getEndPoint(DtwFeature dewFeatemp) {
		int dir = dewFeatemp.getDir();
		Map<String, Object> data = dewFeatemp.getData();
		String testSetRoad = ChangeType.blobChangetoString((byte[]) data.get("POLYLINE"));		//取出测试用的路径
		List<GPSPoint> testSetGpsPoint = ChangeType.changeToObject(testSetRoad);//将坐标转换成二维数组
		if(dir==0){
			Collections.reverse(testSetGpsPoint);
		}
		return testSetGpsPoint.get(testSetGpsPoint.size()-1);
	}
	/**
	 * 得到测试集数据中所经过的路径长度，a到b的长度，没有截取段。
	 * a--------------------b
	 * @param dewFeatemp
	 * @return
	 * @author tomwang
	 */
	public static double getAllroadDistencd (DtwFeature dewFeatemp){
		double reuslt = 0;
		int i;
		Map<String, Object> data = dewFeatemp.getData();
		String testSetRoad = ChangeType.blobChangetoString((byte[]) data.get("POLYLINE"));		//取出测试用的路径
		List<GPSPoint> SetGpsPoint = ChangeType.changeToObject(testSetRoad);//将坐标转换成二维数组
		for(i=1; i<SetGpsPoint.size(); i++){
			reuslt += EuclideanMetric.getEDistence(SetGpsPoint.get(i-1), SetGpsPoint.get(i));
		}
		return reuslt;
	}
	/**
	 * 得到测试集数据中所经过的时间，a到b的时间，没有截取段。
	 * a--------------------b
	 * @param dewFeatemp
	 * @return
	 * @author tomwang
	 */
	public static double getAllroadTime (DtwFeature dewFeatemp){
		double reuslt = 0;
		Map<String, Object> data = dewFeatemp.getData();
		String testSetRoad = ChangeType.blobChangetoString((byte[]) data.get("POLYLINE"));		//取出测试用的路径
		List<GPSPoint> SetGpsPoint = ChangeType.changeToObject(testSetRoad);//将坐标转换成二维数组
		reuslt = (SetGpsPoint.size()-1)*15;
		return reuslt;
	}
	/**
	 * 得到测试集数据中所经过的平均速度，a到b的平均速度，没有截取段。
	 * a--------------------b
	 * @param dewFeatemp
	 * @return
	 * @author tomwang
	 */
	public static double getAllroadAverage (DtwFeature dewFeatemp){
		double result = getAllroadDistencd(dewFeatemp) / getAllroadTime(dewFeatemp);
		if(getAllroadTime(dewFeatemp)==0){
			result=0;
		}
		return result;
	}
}
